<?php
include 'php-ofc-library/open-flash-chart.php';
require_once 'bootstrap.php';

global $cur_time; 

//////////////////////////////start of calling database////////////////////////

$dbstat = new DbStat();
date_default_timezone_set("Asia/Singapore");

//creating an array for the past 24hrs breakdown starting from 0-1 hr, 1-2 hr ....
if(isset($_GET['sp3']))
	$x_value = $_GET['sp3'];
else
	$x_value = 1;
$dbss = array();

for($i = 0; $i < 24/$x_value; $i++) {
	$dbss[] = $dbstat->getDuration(date('YmdHis',$cur_time+$i*$x_value*60*60), date('YmdHis', $cur_time+($i+1)*$x_value*60*60));
}


$averages = array();
foreach ($dbss as $key => $dbs1) {
	$averages[$key] = 0.0; // initialize
	
	if($dbs1) {
		// if $dbs1 is not empty
		foreach($dbs1 as $res) {
			$averages[$key] += $res['DIC_HR'];
		}
		$averages[$key] /= sizeof($dbs1); // get average
		//var_dump($dbs1);
	}
}

$data = array();
for ($i = 0; $i < 24/$x_value; $i++) {
	$data[] = floatval($averages[$i]);
}

//getting user threshold from the userconfig table
$urs_config = new UserConfig($_SESSION['user']);
$first = $urs_config->getFirst('PCT_RELOADS');
$second = $urs_config->getSecond('PCT_RELOADS');
if ($first > $second) {
	$upper = $first;
	$lower = $second;
} else {
	$upper = $second;
	$lower = $first;
}

///////////////////////////////end of calling database//////////////////////////


///// start of graph //////
$title = new title('Proportion of Reloads');

$y = new y_axis(); 
$y->set_range( 0, 20, 2 );
$x = new x_axis(); 
$x->set_range( 1, 24/$x_value, 1 );
$x_labels = new x_axis_labels();
$x_label_arr = array();
for ($i = 0; $i <= 24/$x_value; $i++) {
	$x_label_arr[] = (string)(($i) * $x_value);
}

$x_labels->set_labels(
    $x_label_arr
    );
$x->set_labels($x_labels);

$x_legend = new x_legend( 'no. of hours' );
$x_legend->set_style( '{font-size: 16px; color: #FFFFFF}' );
$y_legend = new y_legend( 'ratio' );
$y_legend->set_style( '{font-size: 16px; color: #FFFFFF}' );

$bar_value_arr = array(); 
foreach($data as $value)
{
	$barValue = new bar_value($value);
	if($value <= $lower ) {
	$barValue->set_colour('#00FF00'); //green
	}
	elseif ($value > $lower && $value <= $upper) {
	$barValue->set_colour('#FFFF00'); //yellow
	}
	else {
	$barValue->set_colour('#FF0000'); //red
	}
	$bar_value_arr[]=  $barValue;
}

$bar = new bar_glass();
$bar->set_values($bar_value_arr );
$bar->set_on_click('submitToReport3');
$bar->key('healthy', 12);
$bar->set_colour('#00FF00');

$empty = new line();
$empty->set_key('not so healthy', 12);
$empty->set_colour('#FFFF00');

$empty1 = new line();
$empty1->set_key('unhealthy', 12);
$empty1->set_colour('#FF0000');

$chart = new open_flash_chart();
$chart->set_title( $title );
$chart->add_element( $bar );
$chart->add_element( $empty );
$chart->add_element( $empty1 );
$chart->set_x_axis( $x );
$chart->set_y_axis( $y );
$chart->set_x_legend( $x_legend );
$chart->set_y_legend( $y_legend );
$chart->set_bg_colour( '#736F6E' );

echo $chart->toPrettyString();
?>